#!/bin/bash

rm -f out*.txt

ulimit -t 600
ulimit -v 2000000

#./ub_test.sh small.c > /dev/null &&\
#n=`echo $?`

if 
  clang-6.0 -pedantic -fsanitize=undefined -Wall -o small11 small.c  >out.txt 2>&1 &&\
  ! grep 'conversions than data arguments' out.txt &&\
  ! grep 'incompatible redeclaration' out.txt &&\
  ! grep 'ordered comparison between pointer' out.txt &&\
  ! grep 'eliding middle term' out.txt &&\
  ! grep 'end of non-void function' out.txt &&\
  ! grep 'invalid in C99' out.txt &&\
  ! grep 'specifies type' out.txt &&\
  ! grep 'should return a value' out.txt &&\
  ! grep 'uninitialized' out.txt &&\
  ! grep 'incompatible pointer to' out.txt &&\
  ! grep 'incompatible integer to' out.txt &&\
  ! grep 'type specifier missing' out.txt &&\
  ./small11 >& out-small-clang.txt &&\
  ! grep 'error' out-small-clang.txt &&\
  gcc -fsanitize=undefined -Wall -Wextra -o small22 small.c >outa.txt 2>&1 &&\
  ! grep uninitialized outa.txt &&\
  ! grep 'without a cast' outa.txt &&\
  ! grep 'control reaches end' outa.txt &&\
  ! grep 'return type defaults' outa.txt &&\
  ! grep 'cast from pointer to integer' outa.txt &&\
  ! grep 'useless type name in empty declaration' outa.txt &&\
  ! grep 'no semicolon at end' outa.txt &&\
  ! grep 'type defaults to' outa.txt &&\
  ! grep 'too few arguments for format' outa.txt &&\
  ! grep 'incompatible pointer' outa.txt &&\
  ! grep 'ordered comparison of pointer with integer' outa.txt &&\
  ! grep 'declaration does not declare anything' outa.txt &&\
  ! grep 'expects type' outa.txt &&\
  ! grep 'pointer from integer' outa.txt &&\
  ! grep 'incompatible implicit' outa.txt &&\
  ! grep 'excess elements in struct initializer' outa.txt &&\
  ! grep 'comparison between pointer and integer' outa.txt &&\
  ./small22 >& out-small-gcc.txt &&\
  ! grep 'error' out-small-gcc.txt &&\
  #./ub_test.sh small.c > /dev/null &&\
  #n=`echo $?`
  clang-11 -w -O1 small.c -o small1 >/dev/null 2>&1 &&\
  ./small1 > out1.txt 2>&1 &&\

  clang-11 -w -O2 small.c -o small2 >/dev/null 2>&1 &&\
  ./small2 > out2.txt 2>&1 &&\
  ! diff out1.txt out2.txt
then
    ./ub_test.sh small.c >/dev/null
    n=`echo $?`
    if [ $n == 0 ]; then
    exit 0
    fi
else
  exit 1
fi


